Passed
Push — master ( 1a1332...54798a )
by Andreas
18:40
created

subform.js ➔ init_subform   D

Complexity

Conditions 12

Size

Total Lines 54
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 34
c 0
b 0
f 0
dl 0
loc 54
rs 4.8

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like subform.js ➔ init_subform often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
function init_subform(id, sortable) {
2
    var container = $('#' + id),
3
        delete_button = $('<a class="button remove-item">-</a>'),
4
        add_button = $('<a class="button add-item">+</a>')
5
            .on('click', function(e) {
6
                e.preventDefault();
7
                add_form(container, add_button, delete_button, sortable);
8
            }),
9
        index = 0;
10
11
    container.on('click', 'a.remove-item', function(e) {
12
        e.preventDefault();
13
        $(this).parent().remove();
14
        if (   container.data('max-count') > 0
15
            && container.data('max-count') >= container.find('fieldset').length
16
            && container.find('.add-item').length === 0) {
17
            container.append(add_button);
18
        }
19
    });
20
21
    container.children().each(function() {
22
        $(this).prepend(delete_button.clone());
23
        index++;
24
    });
25
26
    container.data('index', index);
27
    if (   container.data('max-count') === 0
28
        || container.data('max-count') > index) {
29
        container.append(add_button);
30
    }
31
32
    if (sortable === true) {
33
        container
34
            .sortable({items: '> :not(a.add-item)'})
35
            .on('sortupdate', function() {
36
                $($(this).find('> .ui-sortable-handle').get().reverse()).each(function(index, element) {
37
                    let id = element.id
38
                    if (!id) {
39
                        id = $('> .input > *:first-child', element).attr('id');
40
                    }
41
                    $('#' + id + '_score').val(index);
42
                });
43
            });
44
    }
45
46
    add_button.on('click', function() {
47
        // If there is exactly one file selector, we're probably in some sort of attachment list,
48
        // so let's assume the user wants to add a file
49
        // (at some point this should probably be made configurable)
50
        if ($(this).prev().find('input[type="file"]').length === 1) {
51
            $(this).prev().find('input[type="file"]').click();
52
        }
53
    });
54
}
55
56
function add_form(container, add_button, delete_button, sortable) {
57
    var prototype = container.data('prototype'),
58
        index = container.data('index'),
59
        new_form = prototype.replace(/__name__/g, index);
60
    container.data('index', index + 1);
61
    $(new_form)
62
        .prepend(delete_button.clone())
63
        .insertBefore(add_button);
64
65
    if (   container.data('max-count') > 0
66
        && container.data('max-count') >= container.find('> :not(.button.add-item)').length) {
67
        add_button.detach();
68
    }
69
    if (sortable === true) {
70
        container.sortable('refresh');
71
        container.trigger('sortupdate');
72
    }
73
}
74